package com.wu.client.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.wu.client.cache.featuredData.FeaturedAlbumCache;
import com.wu.client.cache.featuredData.FeaturedArtistCache;
import com.wu.client.cache.featuredData.FeaturedMvCache;
import com.wu.client.constant.FansTypeEnum;
import com.wu.client.constant.ResTypeEnum;
import com.wu.client.dao.redis.Likes;
import com.wu.client.dto.AlbumMongo;
import com.wu.client.dto.ArtistMongo;
import com.wu.client.dto.MvMongo;
import com.wu.client.service.FeaturedService;
import com.wu.client.service.UserLikesService;
import com.wu.client.utils.PageRespUtil;
import com.wu.client.vo.req.album.AlbumReq;
import com.wu.client.vo.req.artist.ArtistReq;
import com.wu.client.vo.res.PageResp;
import com.wu.client.vo.res.album.AlbumBaseResp;
import com.wu.client.vo.res.artist.ArtistBaseResp;
import com.wu.client.vo.res.mv.MvBaseResp;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author Cousin
 * @date 2025/3/28
 * @Description
 */
@Service
@RequiredArgsConstructor
public class FeaturedServiceImpl implements FeaturedService {
    private final UserLikesService userLikesService;

    @Override
    public PageResp<ArtistBaseResp> getFeaturedArtists(ArtistReq req) {
        Integer page = req.getPage();
        Integer limit = req.getLimit();
        Long userId = req.getUserId();
        FeaturedArtistCache featuredArtistCache = FeaturedArtistCache.getInstance();
        List<ArtistMongo> artistMongoList = featuredArtistCache.getInPage(page, limit);
        if (CollectionUtil.isEmpty(artistMongoList)){
            return new PageResp<>();
        }
        List<ArtistBaseResp> artistBaseRespList = BeanUtil.copyToList(artistMongoList, ArtistBaseResp.class);
        List<Long> artistIds = artistBaseRespList.stream().map(ArtistBaseResp::getId).toList();
        Map<Long, Likes> userIfFans = userLikesService.checkUserIfFans(FansTypeEnum.ARTIST, userId, artistIds);
        for (ArtistBaseResp artistBaseResp : artistBaseRespList) {
            Long id = artistBaseResp.getId();
            Likes likes = userIfFans.get(id);
            if (ObjectUtil.isNotNull(likes)){
                artistBaseResp.setIsFollowed(likes.getIsLiked());
                artistBaseResp.setFollowerCount(likes.getCount());
            }
        }
        long total = featuredArtistCache.getTotal();
        return PageRespUtil.buildPageResp(page, limit, total, artistBaseRespList);
    }

    @Override
    public PageResp<AlbumBaseResp> getFeaturedAlbums(AlbumReq req) {
        Integer page = req.getPage();
        Integer limit = req.getLimit();
        FeaturedAlbumCache featuredAlbumCache = FeaturedAlbumCache.getInstance();
        List<AlbumMongo> albumMongoList = featuredAlbumCache.getInPage(page,limit);
        List<AlbumBaseResp> albumBaseRespList = BeanUtil.copyToList(albumMongoList, AlbumBaseResp.class);
        long total = featuredAlbumCache.getTotal();
        return PageRespUtil.buildPageResp(page,limit,total,albumBaseRespList);
    }

    @Override
    public PageResp<MvBaseResp> getFeaturedMvs(Integer page, Integer limit, Long userId) {
        FeaturedMvCache instance = FeaturedMvCache.getInstance();
        List<MvMongo> mvMongoList = instance.getInPage(page, limit);
        if (CollectionUtil.isNotEmpty(mvMongoList)){
            List<MvBaseResp> mvBaseRespList = BeanUtil.copyToList(mvMongoList, MvBaseResp.class);
            List<Long> mvIds = mvBaseRespList.stream().map(MvBaseResp::getId).toList();
            Map<Long, Likes> longLikesMap = userLikesService.checkUserIfLike(ResTypeEnum.MV, userId, mvIds);
            for (MvBaseResp item : mvBaseRespList) {
                Likes likes = longLikesMap.get(item.getId());
                if (ObjectUtil.isNotNull(likes)){
                    item.setIsLiked(likes.getIsLiked());
                    item.setLikesCount(likes.getCount());
                }
            }
            return PageRespUtil.buildPageResp(page,limit,instance.getTotal(),mvBaseRespList);
        }

        return new PageResp<>() ;
    }
}
